//Copyright (C) 2011 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

/*
 * HandoffPopupDialog2.java
 *
 * Created on Jan 22, 2011, 11:37:45 AM
 */

package org.workplicity.cia.ui;

import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.workplicity.cia.util.CIAHelper;
import org.workplicity.elog.entry.Elog;
import org.workplicity.elog.entry.ElogUser;
import org.workplicity.elog.entry.Handoff;
import org.workplicity.elog.repos.Handoffs;
import org.workplicity.util.DateFormatter;
import org.workplicity.util.Helper;
import org.workplicity.util.WorkDate;
import org.workplicity.worklet.WorkletContext;

/**
 * This dialog implements the handoff popup.
 * @author Ron Coleman
 */
public class HandoffPopupDialog2 extends javax.swing.JDialog {
    private WorkletContext context = WorkletContext.getInstance();

    private ElogUser me = (ElogUser) Helper.whoAmI(context);

    private Handoff handoff;

    /** Creates new form HandoffPopupDialog2 */
    public HandoffPopupDialog2(JDialog parent, Handoff handoff) {
        super(parent, true);
        initComponents();

        this.setTitle("Netlog (" + me.getLogname() + ") >> Handoff >> "
                + (handoff.getId()==-1 ? "Create" : "View"));

        this.setLocationRelativeTo(null);

        this.handoff = handoff;

        init();
    }

    /*
     * Constructor
     * @param parent Parent frame
     * @param handoff Initial handoff
     */
    public HandoffPopupDialog2(Frame parent, Handoff handoff) {
        super(parent, true);
        initComponents();

        this.setTitle("Netlog (" + me.getLogname() + ") >> Handoff");

        this.setLocationRelativeTo(null);

        this.handoff = handoff;

        init();
    }


    /**
     * Initialize the display with the defaults or the input handoff.
     */
    private void init() {
        Integer fromId = handoff.getFromId();
        this.fromTextField.setText(""+CIAHelper.toUser(fromId));

        Integer toId = handoff.getToId();
        Integer tradeId = handoff.getTradeId();

        if(toId != -1 && tradeId == -1) {
            this.toTextField.setText(""+CIAHelper.toUser(toId));
            this.tradeTextField.setVisible(false);
        }
        else if(toId == -1 && tradeId != -1) {
            this.toTextField.setText(""+CIAHelper.toTrade(tradeId));
            this.tradeTextField.setVisible(false);
        }
        else {
            this.toTextField.setText(""+CIAHelper.toUser(toId));
            this.tradeTextField.setText(""+CIAHelper.toTrade(tradeId));
        }

        // If this is an established handoff
        // disable the message area and the create
        // button and change the text of the "skip" button
        if(handoff.getId() != -1)
        {
            this.handoffIdTextField.setText(handoff.getId()+"");
            this.dateTextField.setText(DateFormatter.toString(handoff.getUpdateDate()));
            this.descriptionTextArea.setEnabled(false);
            this.createButton.setVisible(false);
            this.skipButton.setText("Okay");
        }
        else {
            this.handoffIdTextField.setText("NA");
            this.dateTextField.setText(DateFormatter.toString(new WorkDate()));
            this.logEntryButton.setEnabled(false);
        }

        this.descriptionTextArea.setText(handoff.getMessage());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        fromTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        toTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        dateTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        handoffIdTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tradeTextField = new javax.swing.JTextField();
        logEntryButton = new javax.swing.JButton();
        createButton = new javax.swing.JButton();
        skipButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("From:");

        fromTextField.setEditable(false);
        fromTextField.setFont(new java.awt.Font("Tahoma", 0, 12));
        fromTextField.setText("Dave");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("To:");

        toTextField.setEditable(false);
        toTextField.setText("Mark");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Date:");

        dateTextField.setEditable(false);
        dateTextField.setText("01 / 15 / 2011");

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setFont(new java.awt.Font("Tahoma", 0, 12));
        descriptionTextArea.setRows(5);
        descriptionTextArea.setText("N/A");
        jScrollPane1.setViewportView(descriptionTextArea);

        handoffIdTextField.setEditable(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Id:");

        tradeTextField.setEditable(false);
        tradeTextField.setFont(new java.awt.Font("Tahoma", 0, 12));
        tradeTextField.setText("HVAC");

        logEntryButton.setFont(new java.awt.Font("Tahoma", 0, 10));
        logEntryButton.setText("Log entry");
        logEntryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logEntryButtonActionPerformed(evt);
            }
        });

        createButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        createButton.setText("Create");
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        skipButton.setFont(new java.awt.Font("Tahoma", 0, 12));
        skipButton.setText("Skip");
        skipButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                skipButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Message:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 257, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(logEntryButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(toTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fromTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(handoffIdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tradeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(createButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(skipButton, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(handoffIdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fromTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(toTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tradeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(logEntryButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(skipButton)
                    .addComponent(createButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void logEntryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logEntryButtonActionPerformed
        final JDialog parent = this;

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                String reposTitle = handoff.getReposTitle();

                Integer id = handoff.getElogId();

                Elog elog = (Elog) Helper.fetch(reposTitle,id,context);

                ElogUser owner = CIAHelper.toUser(reposTitle);
                ElogEditDialog dialog = new ElogEditDialog(parent,owner,elog);

                dialog.setVisible(true);
            }
        });
}//GEN-LAST:event_logEntryButtonActionPerformed

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        final JDialog dialog = this;

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                handoff.setMessage(descriptionTextArea.getText());

                if(!Helper.insert(handoff, Handoffs.TITLE, context))
                    JOptionPane.showMessageDialog(dialog, "Wow, handoff creation failed!",
                            "Netlog", JOptionPane.ERROR_MESSAGE);

                dialog.dispose();

                return;
            }

        });
    }//GEN-LAST:event_createButtonActionPerformed

    private void skipButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_skipButtonActionPerformed
        this.dispose();
}//GEN-LAST:event_skipButtonActionPerformed

//    /**
//    * @param args the command line arguments
//    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                HandoffPopupDialog2 dialog = new HandoffPopupDialog2(null, null);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createButton;
    private javax.swing.JTextField dateTextField;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JTextField fromTextField;
    private javax.swing.JTextField handoffIdTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton logEntryButton;
    private javax.swing.JButton skipButton;
    private javax.swing.JTextField toTextField;
    private javax.swing.JTextField tradeTextField;
    // End of variables declaration//GEN-END:variables

}
